//
//  CallToWatchStateVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 2016/12/30.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "CallToWatchStateVC.h"
#import "NetTools.h"
#import "BindTool.h"
#import "HttpTool.h"
@interface CallToWatchStateVC ()
@property (weak, nonatomic) IBOutlet UIImageView *headPic;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UILabel *address;
@property (weak, nonatomic) IBOutlet UILabel *des1;
@property (weak, nonatomic) IBOutlet UILabel *des2;
@property (weak, nonatomic) IBOutlet UIView *staus;
@property (weak, nonatomic) IBOutlet UIImageView *watch;
@property (weak, nonatomic) IBOutlet UIImageView *connect;
@property (weak, nonatomic) IBOutlet UIImageView *unConnect;
- (IBAction)back:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *goback;

@end

@implementation CallToWatchStateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack:) name:@"receive_call_back" object:nil];
    self.navigationController.navigationBarHidden=YES;
    _phone.text=_phoneValue;
    if (![NetTools share].isHaveInternet) {
        [self setNoIntenertStatus];
    }else{
        _des1.text=@"正在通知wphone拨号";
        [self sendCallCmd];
        [self getAddress];
    }
}
-(void)callBack:(id)sender{
    [self HavingReciving];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setNoIntenertStatus{
    _staus.hidden=NO;
    _unConnect.hidden=NO;
    _connect.hidden=YES;
    _des2.hidden=NO;
    _des2.text=@"请检查网络是否断开";
    _des1.text=@"与wphone连接失败";
}
-(void)HavingReciving{
    _staus.hidden=YES;
    _watch.hidden=NO;
    _des2.hidden=NO;
    _des1.text=@"拨号指令已送达";
    _des2.text=@"请在wphone上接听";
}
-(void)sendCallCmd{
    NSDictionary *data =@{@"phone":_phoneValue};
    EMMessage *message = [EaseSDKHelper sendCmdMessage1:@"call_phone" to:[BindTool share].bindInfo.phone_number messageType: EMConversationTypeChat messageExt:nil cmdParams:nil data:data];
    [[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:^(EMMessage *aMessage, EMError *aError) {
        if (!aError) {
            
        }
        else {
          
        }
    }];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:YES];
     [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)getAddress{
    __weak typeof(self) weakSelf = self;
    NSString *url =@"http://op.juhe.cn/onebox/phone/query?tel=";
    url =[url stringByAppendingString:_phoneValue];
    NSDictionary *dic =@{@"key":@"f0bccf4d1690f661bb1a4d1dbf71d0ef",@"dtype":@"json"};
    [HttpTool GET:url WithHeader:NO parameters:dic success:^(id responseObject) {
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"getBing====%@",str);
        NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseObject
                                                             options:NSJSONReadingMutableContainers
                                                               error:nil];
        NSString *reason =[data objectForKey:@"reason"];
        if ([reason isEqualToString:@"查询成功"]) {
            NSDictionary *result =[data objectForKey:@"result"];
            NSString *province =[result  objectForKey:@"province"];
            NSString *city =[result  objectForKey:@"city"];
            NSString *sp =[result  objectForKey:@"sp"];
            weakSelf.address.text= [province stringByAppendingString:city];
        }else{
            weakSelf.address.text=@"未知";
        }
    } failure:^(NSError *error) {
        
    }];
}
@end

